<template>
    <div class="login">
        <div class="lTitle">
            <img src="../../static/images/logo02.png" alt="" />
            <h2>login</h2>
        </div>

        <van-form @submit="sendLogin">
            <van-field v-model="username" name="username" label="用户名" placeholder="用户名" :rules="[{ required: true, message: '请填写用户名' }]" />
            <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]" />
            <div style="margin: 16px">
                <van-button round block type="info" native-type="submit">登录</van-button>
            </div>
        </van-form>
        <router-link class="goRegister" replace to="/register">去注册</router-link>
    </div>
</template>

<script>
export default {
    data() {
        return {
            username: "",
            password: "",
        };
    },
    methods: {
        sendLogin(values) {
            if (this.username != "" && this.password != "") {
                let token = JSON.parse(localStorage.getItem("token"));
                if (token) {
                    let iIndex = token.find((item) => {
                        return item.username == this.username;
                    });
                    if (!iIndex) {
                        alert("用户未注册");
                        if (confirm("是否前往注册页面?")) {
                            this.$router.replace("/register");
                        } else {
                            return;
                        }
                    } else {
                        if (this.password == iIndex.password) {
                            this.$toast("登录成功");
                            // alert("登录成功");
                            localStorage.setItem("pass", 1);
                            localStorage.setItem("loginUser", JSON.stringify(iIndex));
                            this.$router.go(-1);
                        } else {
                            alert("密码错误");
                        }
                    }
                } else {
                    alert("用户未注册");
                    if (confirm("是否前往注册页面?")) {
                        this.$router.replace("/register");
                    } else {
                        return;
                    }
                }
            }
        },
    },
};
</script>

<style lang="scss" scope>
@import url(../../static/style/reset.css);
@import "../../static/style/variable.scss";
.login {
    overflow: hidden;
    .lTitle {
        font-size: 1.28rem;
        text-align: center;
        padding-bottom: 1.3333rem;
        margin-top: 3.2rem;
        font-weight: 800;
        color: $activeColor;
        display: flex;
        justify-content: center;
        img {
            width: 1.3333rem;
        }
    }
    .goRegister {
        display: block;
        font-size: 0.32rem;
        text-align: center;
    }
    .van-button--info {
        color: white;
        background-color: $bgColor;
        border: 1px solid $activeColor;
        width: 100%;
        margin: 0 auto;
    }
    .van-form {
        margin: 0 0.2667rem;
    }
}
</style>
